﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RentIt06WPFClient.UserSession;

namespace RentIt06WPFClient
{
    /// <summary>
    /// Interaction logic for FrontPage.xaml
    /// </summary>
    public partial class FrontPage : Page
    {
        private readonly SearchParameters PopularSearch = new SearchParameters {
            OrderBy = SearchOrder.NumberOfPurchases,
            ResultLimit = 10
        };

        public FrontPage() {
            InitializeComponent();
            // Adding new styles as the old ones did not have Setters.
            // This could proberbly be done in xaml but it was rather complicated
            // when we use a custom control.
            PopularList.List.ItemContainerStyle = new Style();
            SearchList.List.ItemContainerStyle = new Style();
            DownloadList.List.ItemContainerStyle = new Style();
            // Adding double click events to the lists.
            PopularList.List.ItemContainerStyle.Setters.Add(new EventSetter()
            {
                Event = Control.MouseDoubleClickEvent,
                Handler = new MouseButtonEventHandler(PopularList_MouseDoubleClick)
            });
            SearchList.List.ItemContainerStyle.Setters.Add(new EventSetter()
            {
                Event = Control.MouseDoubleClickEvent,
                Handler = new MouseButtonEventHandler(SearchList_MouseDoubleClick)
            });
            DownloadList.List.ItemContainerStyle.Setters.Add(new EventSetter()
            {
                Event = Control.MouseDoubleClickEvent,
                Handler = new MouseButtonEventHandler(DownloadList_MouseDoubleClick)
            });
        }

        private void Page_Loaded(object sender, RoutedEventArgs e) {
            PopularList.ItemsSource = Session.User.SearchProduct(PopularSearch);
            DownloadList.ItemsSource = Session.User.ReadDownloadableProducts();
            SearchButton_Click(null, null);
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e) {
            SearchAvailability availability;
            switch (AvailabilityBox.SelectedIndex) {
                case 0:
                    availability = SearchAvailability.CanBeRented;
                    break;
                case 1:
                    availability = SearchAvailability.CanBeBought;
                    break;
                default:
                    availability = SearchAvailability.Both;
                    break;
            }
            SearchOrder orderby;
            switch (OrderByBox.SelectedIndex) {
                case 0:
                    orderby = SearchOrder.Title;
                    break;
                case 1:
                    orderby = SearchOrder.NumberOfPurchases;
                    break;
                default:
                    orderby = SearchOrder.ReleaseDate;
                    break;
            }
            Media[] searchResult = Session.User.SearchProduct(new SearchParameters {
                SearchString = SearchBox.Text,
                Availability = availability,
                OrderBy = orderby
            });
            SearchList.ItemsSource = searchResult;
        }

        private void SearchList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            var media = (Media)((ListBoxItem)sender).DataContext;
            NavigationService.Navigate(new MediaView(media));
        }

        private void PopularList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            var media = (Media)((ListBoxItem)sender).DataContext;
            NavigationService.Navigate(new MediaView(media));
        }

        private void DownloadList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            var media = (Media)((ListBoxItem)sender).DataContext;
            NavigationService.Navigate(new MediaView(media));
        }
    }
}
